App.controller('mediaController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {
	
	$scope.getGridName = function(parentId){
		 return 'mediaGrid_' + parentId;
	}
	
	 Page.ajaxPostQuiet('mediaType/queryTopType.do', null, function (resp) {
        $scope.parentMedias = resp.data;
        $($scope.parentMedias).each(function(index, parent) {
        	var gridName = $scope.getGridName(parent.mediaId);
	       	$scope[gridName] = angular.extend({}, $scope.mediaGrid);
	       	$scope[gridName].gName = gridName;
	   	});
        $scope.parentMedias[0].active = true;
    });
	
	
    $scope.mediaGrid = {
		options: {
            paging: false,
            bAutoWidth:false
        },
        column_defs: [
            {
                target: 3,
                buttons: [
                    {
                        name: '失效',
                        clazz: 'btn btn-delete',
                        icon: 'fa fa-trash-o',
                        event: function(row){
                    		Page.ajaxPost('mediaType/'+row.mediaId+'/disable.do', row, function () {
                    			var gridName = $scope.getGridName(row.parentId);
                    			$scope[gridName].query();
			                    $scope.mediaGrid.query();
			                });
                        },
                        visiable : function(row) {
                            return row.editable == "1" && row.status == "1";
                        }
                    },
                    {
                        name: '生效',
                        clazz: 'btn btn-success',
                        icon: 'fa-undo',
                        event: function(row){
                    		Page.ajaxPost('mediaType/'+row.mediaId+'/revert.do', row, function () {
			                    var gridName = $scope.getGridName(row.parentId);
                    			$scope[gridName].query();
			                    $scope.mediaGrid.query();
			                });
                        },
                        visiable : function(row) {
                            return row.editable == "1" && row.status == "0";
                        }
                    }
                ]
            }
        ],
        add : function(parent) {
            $scope.mediaType = {};
            $scope.mediaType.parentId = parent.mediaId;
            $scope.mediaTypeModal.show();
        }
    }
    
    $scope.mediaTypeModal = {
    	onOkClick : function() {
	    	if(!$scope.validateForm.$valid) {
            	$scope.validateForm.doValidate();
	        } else {
	        	Page.ajaxPost('mediaType/save.do', $scope.mediaType, function (resp) {
	                    var gridName = $scope.getGridName($scope.mediaType.parentId);
                    	$scope[gridName].query();
	                    $scope.mediaTypeModal.hide();
	            });
	        }
    	},
    	onHidden : function() {
        	$scope.validateForm.reset();
    	},
    };

    $scope.busTypeGrid = {
		options: {
            paging: false,
            bAutoWidth:false
        },
        column_defs: [
            {
                target: 4,
                buttons: [
                    {
                        name: '编辑',
                        clazz: 'btn-edit',
                        icon: 'fa-edit',
                        event: 'update'
                    },
                    {
                        name: '失效',
                        clazz: 'btn btn-delete',
                        icon: 'fa fa-trash-o',
                        event: function(row){
                    		Page.ajaxPost('busType/'+ row.typeId +'/disable.do', row, function () {
			                    $scope.busTypeGrid.query();
			                });
                        },
                        visiable: function(row) {
                            return row.status == "1";
                        }
                    },
                    {
                        name: '生效',
                        clazz: 'btn btn-success',
                        icon: 'fa-undo',
                        event: function(row){
                    		Page.ajaxPost('busType/'+ row.typeId +'/revert.do', row, function () {
			                    $scope.busTypeGrid.query();
			                });
                        },
                        visiable: function(row) {
                            return row.status != "1";
                        }
                    }
                ]
            }
        ],
        add : function() {
            $scope.busType = {};
            $scope.busTypeAddFlag = true;
            $scope.busTypeModal.show();
        },
        update : function(row) {
            $scope.busType = row;
            $scope.busType.file = {};
            $scope.busType.file.path = $scope.busType.descImg;
            $scope.busTypeModal.show();
        }
    }
    

    
    $scope.busTypeModal = {
		onShow : function(){
	      	$scope.busType.file = {
				path: $scope.busType.descImg
			};
   		},
   		onOkClick : function() {
	    	if(!$scope.validateBusForm.$valid) {
            	$scope.validateBusForm.doValidate();
	        } else {
	        	$scope.busType.descImg = $scope.busType.file.path;
	            if($scope.busTypeAddFlag) { //添加
	                Page.ajaxPost('busType/add.do', $scope.busType, function (resp) {
	                    $scope.busTypeGrid.query();
	                    $scope.busTypeAddFlag = false;
	                    $scope.busTypeModal.hide();
	                });
	            } else {
	                Page.ajaxPost('busType/update.do', $scope.busType, function (resp) {
	                    $scope.busTypeGrid.query();
	                    $scope.busTypeModal.hide();
	                });
	            }
	        }
    	},
    	onHidden : function() {
        	$scope.validateBusForm.reset();
    	},
	};


}]);
